package TestCode;

import dao.OrderMapper;
import dao.StudentsMapper;
import dao.UserMapper;
import dao.UserRoleMapper;
import javaBean.Order;
import javaBean.student;
import javaBean.user;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import tools.initLogRecord;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class TestDb {

    private static InputStream in;
    private static SqlSessionFactory factory;
    private static SqlSession sqlSession;


    public static void init() throws IOException {
        //1.读取加载核心配置文件
        in = Resources.getResourceAsStream("mybatis-config.xml");
        //2.构造会话工厂
        factory = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        sqlSession = factory.openSession();

    }

    @Test
    public void testOneToOne() throws IOException {
        initLogRecord.initLog();
        init();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<Order> all = mapper.findAll();
        for (Order o : all) {
            System.out.println("o = " + o);
        }
    }

    @Test
    public void testOneTomore() throws IOException {
        initLogRecord.initLog();
        init();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<user> all = mapper.findAll1();
        for (user o : all) {
            System.out.println("o = " + o);
        }
    }

    @Test
    public void testmoreTomore() throws IOException {
        initLogRecord.initLog();
        init();
        UserRoleMapper mapper = sqlSession.getMapper(UserRoleMapper.class);
        List<user> all = mapper.findUAR();
        for (user o : all) {
            System.out.println("o = " + o);
        }
    }

    @Test
    public void testfindThree() throws IOException {
        initLogRecord.initLog();
        init();
        StudentsMapper mapper = sqlSession.getMapper(StudentsMapper.class);
        List<student> students = mapper.studentsAll();
        for (student o : students) {
            System.out.println("o = " + o);
        }
    }


}
